package common.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import jhunter.conf.forms.vo.EasyForm;
import jhunter.conf.forms.vo.Element;
import jhunter.dao.DataAccess;
import jhunter.dao.bo.BaseObject;
import jhunter.dao.condition.ExampleCondition;
import jhunter.web.mvc.ActionContext;

public class EasyFormCondition extends ExampleCondition {

	private EasyForm easyForm;

	private DataAccess dataAccess;

	private ActionContext actionContext;

	public EasyFormCondition(EasyForm easyForm, DataAccess dataAccess, ActionContext actionContext) {
		this.easyForm = easyForm;
		this.dataAccess = dataAccess;
		this.actionContext = actionContext;
		super.setEntity(dataAccess.getEntity());
		super.setExample(buildExample());

	}

	private BaseObject buildExample() {
		BaseObject bo = BaseObject.create();
		if (easyForm.getSearchPanel() != null) {
			List<Element> eles = easyForm.getSearchPanel().getElement();
			for (Element element : eles) {
				String property = element.getEntityProperty();
				String pValue = (String) actionContext.getParameters().get(property);
				if (StringUtils.isNotBlank(pValue)) {
					bo.addValue(property, pValue);
				}
			}
		}
		return bo;
	}


	
	
}
